<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>animationdelay.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_async_animationdelay.js.html">animationdelay.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A delayed callback that pegs to the next animation frame
<a name="line17"></a> * instead of a user-configurable timeout.
<a name="line18"></a> *
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.async.AnimationDelay&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.async.Delay&#39;);
<a name="line24"></a>goog.require(&#39;goog.functions&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>// TODO(nicksantos): Should we factor out the common code between this and
<a name="line29"></a>// goog.async.Delay? I&#39;m not sure if there&#39;s enough code for this to really
<a name="line30"></a>// make sense. Subclassing seems like the wrong approach for a variety of
<a name="line31"></a>// reasons. Maybe there should be a common interface?
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * A delayed callback that pegs to the next animation frame
<a name="line35"></a> * instead of a user configurable timeout. By design, this should have
<a name="line36"></a> * the same interface as goog.async.Delay.
<a name="line37"></a> *
<a name="line38"></a> * Uses requestAnimationFrame and friends when available, but falls
<a name="line39"></a> * back to a timeout of goog.async.AnimationDelay.TIMEOUT.
<a name="line40"></a> *
<a name="line41"></a> * For more on requestAnimationFrame and how you can use it to create smoother
<a name="line42"></a> * animations, see:
<a name="line43"></a> * @see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
<a name="line44"></a> *
<a name="line45"></a> * @param {function(number)} listener Function to call when the delay completes.
<a name="line46"></a> *     Will be passed the timestamp when it&#39;s called, in unix ms.
<a name="line47"></a> * @param {Window=} opt_window The window object to execute the delay in.
<a name="line48"></a> *     Defaults to the global object.
<a name="line49"></a> * @param {Object=} opt_handler The object scope to invoke the function in.
<a name="line50"></a> * @constructor
<a name="line51"></a> * @extends {goog.Disposable}
<a name="line52"></a> */
<a name="line53"></a>goog.async.AnimationDelay = function(listener, opt_window, opt_handler) {
<a name="line54"></a>  goog.base(this);
<a name="line55"></a>
<a name="line56"></a>  /**
<a name="line57"></a>   * The function that will be invoked after a delay.
<a name="line58"></a>   * @type {function(number)}
<a name="line59"></a>   * @private
<a name="line60"></a>   */
<a name="line61"></a>  this.listener_ = listener;
<a name="line62"></a>
<a name="line63"></a>  /**
<a name="line64"></a>   * The object context to invoke the callback in.
<a name="line65"></a>   * @type {Object|undefined}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.handler_ = opt_handler;
<a name="line69"></a>
<a name="line70"></a>  /**
<a name="line71"></a>   * @type {Window}
<a name="line72"></a>   * @private
<a name="line73"></a>   */
<a name="line74"></a>  this.win_ = opt_window || window;
<a name="line75"></a>
<a name="line76"></a>  /**
<a name="line77"></a>   * Cached callback function invoked when the delay finishes.
<a name="line78"></a>   * @type {function()}
<a name="line79"></a>   * @private
<a name="line80"></a>   */
<a name="line81"></a>  this.callback_ = goog.bind(this.doAction_, this);
<a name="line82"></a>};
<a name="line83"></a>goog.inherits(goog.async.AnimationDelay, goog.Disposable);
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Identifier of the active delay timeout, or event listener,
<a name="line88"></a> * or null when inactive.
<a name="line89"></a> * @type {goog.events.Key|number|null}
<a name="line90"></a> * @private
<a name="line91"></a> */
<a name="line92"></a>goog.async.AnimationDelay.prototype.id_ = null;
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * If we&#39;re using dom listeners.
<a name="line97"></a> * @type {?boolean}
<a name="line98"></a> * @private
<a name="line99"></a> */
<a name="line100"></a>goog.async.AnimationDelay.prototype.usingListeners_ = false;
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * Default wait timeout for animations (in milliseconds).  Only used for timed
<a name="line105"></a> * animation, which uses a timer (setTimeout) to schedule animation.
<a name="line106"></a> *
<a name="line107"></a> * @type {number}
<a name="line108"></a> * @const
<a name="line109"></a> */
<a name="line110"></a>goog.async.AnimationDelay.TIMEOUT = 20;
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * Name of event received from the requestAnimationFrame in Firefox.
<a name="line115"></a> *
<a name="line116"></a> * @type {string}
<a name="line117"></a> * @const
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.async.AnimationDelay.MOZ_BEFORE_PAINT_EVENT_ = &#39;MozBeforePaint&#39;;
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Starts the delay timer. The provided listener function will be called
<a name="line125"></a> * before the next animation frame.
<a name="line126"></a> */
<a name="line127"></a>goog.async.AnimationDelay.prototype.start = function() {
<a name="line128"></a>  this.stop();
<a name="line129"></a>  this.usingListeners_ = false;
<a name="line130"></a>
<a name="line131"></a>  var raf = this.getRaf_();
<a name="line132"></a>  var cancelRaf = this.getCancelRaf_();
<a name="line133"></a>  if (raf &amp;&amp; !cancelRaf &amp;&amp; this.win_.mozRequestAnimationFrame) {
<a name="line134"></a>    // Because Firefox (Gecko) runs animation in separate threads, it also saves
<a name="line135"></a>    // time by running the requestAnimationFrame callbacks in that same thread.
<a name="line136"></a>    // Sadly this breaks the assumption of implicit thread-safety in JS, and can
<a name="line137"></a>    // thus create thread-based inconsistencies on counters etc.
<a name="line138"></a>    //
<a name="line139"></a>    // Calling cycleAnimations_ using the MozBeforePaint event instead of as
<a name="line140"></a>    // callback fixes this.
<a name="line141"></a>    //
<a name="line142"></a>    // Trigger this condition only if the mozRequestAnimationFrame is available,
<a name="line143"></a>    // but not the W3C requestAnimationFrame function (as in draft) or the
<a name="line144"></a>    // equivalent cancel functions.
<a name="line145"></a>    this.id_ = goog.events.listen(
<a name="line146"></a>        this.win_,
<a name="line147"></a>        goog.async.AnimationDelay.MOZ_BEFORE_PAINT_EVENT_,
<a name="line148"></a>        this.callback_);
<a name="line149"></a>    this.win_.mozRequestAnimationFrame(null);
<a name="line150"></a>    this.usingListeners_ = true;
<a name="line151"></a>  } else if (raf &amp;&amp; cancelRaf) {
<a name="line152"></a>    this.id_ = raf.call(this.win_, this.callback_);
<a name="line153"></a>  } else {
<a name="line154"></a>    this.id_ = this.win_.setTimeout(
<a name="line155"></a>        // Prior to Firefox 13, Gecko passed a non-standard parameter
<a name="line156"></a>        // to the callback that we want to ignore.
<a name="line157"></a>        goog.functions.lock(this.callback_),
<a name="line158"></a>        goog.async.AnimationDelay.TIMEOUT);
<a name="line159"></a>  }
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Stops the delay timer if it is active. No action is taken if the timer is not
<a name="line165"></a> * in use.
<a name="line166"></a> */
<a name="line167"></a>goog.async.AnimationDelay.prototype.stop = function() {
<a name="line168"></a>  if (this.isActive()) {
<a name="line169"></a>    var raf = this.getRaf_();
<a name="line170"></a>    var cancelRaf = this.getCancelRaf_();
<a name="line171"></a>    if (raf &amp;&amp; !cancelRaf &amp;&amp; this.win_.mozRequestAnimationFrame) {
<a name="line172"></a>      goog.events.unlistenByKey(this.id_);
<a name="line173"></a>    } else if (raf &amp;&amp; cancelRaf) {
<a name="line174"></a>      cancelRaf.call(this.win_, /** @type {number} */ (this.id_));
<a name="line175"></a>    } else {
<a name="line176"></a>      this.win_.clearTimeout(/** @type {number} */ (this.id_));
<a name="line177"></a>    }
<a name="line178"></a>  }
<a name="line179"></a>  this.id_ = null;
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * Fires delay&#39;s action even if timer has already gone off or has not been
<a name="line185"></a> * started yet; guarantees action firing. Stops the delay timer.
<a name="line186"></a> */
<a name="line187"></a>goog.async.AnimationDelay.prototype.fire = function() {
<a name="line188"></a>  this.stop();
<a name="line189"></a>  this.doAction_();
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * Fires delay&#39;s action only if timer is currently active. Stops the delay
<a name="line195"></a> * timer.
<a name="line196"></a> */
<a name="line197"></a>goog.async.AnimationDelay.prototype.fireIfActive = function() {
<a name="line198"></a>  if (this.isActive()) {
<a name="line199"></a>    this.fire();
<a name="line200"></a>  }
<a name="line201"></a>};
<a name="line202"></a>
<a name="line203"></a>
<a name="line204"></a>/**
<a name="line205"></a> * @return {boolean} True if the delay is currently active, false otherwise.
<a name="line206"></a> */
<a name="line207"></a>goog.async.AnimationDelay.prototype.isActive = function() {
<a name="line208"></a>  return this.id_ != null;
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/**
<a name="line213"></a> * Invokes the callback function after the delay successfully completes.
<a name="line214"></a> * @private
<a name="line215"></a> */
<a name="line216"></a>goog.async.AnimationDelay.prototype.doAction_ = function() {
<a name="line217"></a>  if (this.usingListeners_ &amp;&amp; this.id_) {
<a name="line218"></a>    goog.events.unlistenByKey(this.id_);
<a name="line219"></a>  }
<a name="line220"></a>  this.id_ = null;
<a name="line221"></a>
<a name="line222"></a>  // We are not using the timestamp returned by requestAnimationFrame
<a name="line223"></a>  // because it may be either a Date.now-style time or a
<a name="line224"></a>  // high-resolution time (depending on browser implementation). Using
<a name="line225"></a>  // goog.now() will ensure that the timestamp used is consistent and
<a name="line226"></a>  // compatible with goog.fx.Animation.
<a name="line227"></a>  this.listener_.call(this.handler_, goog.now());
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/** @override */
<a name="line232"></a>goog.async.AnimationDelay.prototype.disposeInternal = function() {
<a name="line233"></a>  this.stop();
<a name="line234"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line235"></a>};
<a name="line236"></a>
<a name="line237"></a>
<a name="line238"></a>/**
<a name="line239"></a> * @return {?function(function(number)): number} The requestAnimationFrame
<a name="line240"></a> *     function, or null if not available on this browser.
<a name="line241"></a> * @private
<a name="line242"></a> */
<a name="line243"></a>goog.async.AnimationDelay.prototype.getRaf_ = function() {
<a name="line244"></a>  var win = this.win_;
<a name="line245"></a>  return win.requestAnimationFrame ||
<a name="line246"></a>      win.webkitRequestAnimationFrame ||
<a name="line247"></a>      win.mozRequestAnimationFrame ||
<a name="line248"></a>      win.oRequestAnimationFrame ||
<a name="line249"></a>      win.msRequestAnimationFrame ||
<a name="line250"></a>      null;
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/**
<a name="line255"></a> * @return {?function(number): number} The cancelAnimationFrame function,
<a name="line256"></a> *     or null if not available on this browser.
<a name="line257"></a> * @private
<a name="line258"></a> */
<a name="line259"></a>goog.async.AnimationDelay.prototype.getCancelRaf_ = function() {
<a name="line260"></a>  var win = this.win_;
<a name="line261"></a>  return win.cancelRequestAnimationFrame ||
<a name="line262"></a>      win.webkitCancelRequestAnimationFrame ||
<a name="line263"></a>      win.mozCancelRequestAnimationFrame ||
<a name="line264"></a>      win.oCancelRequestAnimationFrame ||
<a name="line265"></a>      win.msCancelRequestAnimationFrame ||
<a name="line266"></a>      null;
<a name="line267"></a>};
</pre>


</body>
</html>
